/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.sigmatrix.h5.dao.EnterpriseDao;
import com.sigmatrix.h5.entity.Enterprise;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;

/**
 *@ClassName: EnterpriseDaoImpl
 *@Description: 企业
 *@author MH
 *@date 2017年8月17日 上午11:21:28
 */
@Repository
public class EnterpriseDaoImpl extends DaoImpl<Enterprise,Integer> implements EnterpriseDao{

	/**
	 * (non-Javadoc)
	 *@Title: findByCode
	 *@Description: 
	 *@see com.sigmatrix.sc.inspection.inter.dao.EnterpriseDao#findByCode(java.lang.String)
	 *@author MH
	 *@date 2017年9月22日 下午5:40:50
	 */
	@Override
	public Enterprise findByCode(String enterprideCode) {
		Enterprise t = new Enterprise();
		t.setCode(enterprideCode);
		return selectOne(t);
	}
	
	/**表名*/
	private static final String TABLE_NAME = "_enterprise";
	
	@Override
	public Enterprise findById(final Integer id) {
		final StringBuilder sql = new StringBuilder("SELECT * FROM ").append(TABLE_NAME).append(" WHERE _id = ?");
		final Object[] params = new Object[] { id };
		final List<Enterprise> list = jdbcTemplate.query(sql.toString(), params, new EnterpriseRowMapper());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<Enterprise> findListByCondition(final Enterprise enterprise) throws DaoException {

		final StringBuilder sql = new StringBuilder("SELECT * FROM _enterprise");
		final List<Object> paramList = new ArrayList<Object>();
		sql.append(" WHERE _status = ?");
		paramList.add(Cons.STATUS_ENABLE);
		try {
			if(enterprise!=null) {
				if(enterprise.getBasicModuleId()!=null){
					sql.append(" AND _basic_module_id = ?");
					paramList.add(enterprise.getBasicModuleId());
				}
				if(StringUtils.isNotBlank(enterprise.getCode())) {
					sql.append(" AND _code = ?");
					paramList.add(enterprise.getCode());
				}
			}
			return jdbcTemplate.query(sql.toString(), paramList.toArray(), new EnterpriseRowMapper());
		} catch (DataAccessException e) {
			throw new DaoException(e);
		}
	}

	/**
	 * @ClassName: EnterpriseRowMapper
	 * @Description: 封装返回的实体
	 * @author youjun @date 2017-08-03 11:01:33
	 */
	public class EnterpriseRowMapper implements RowMapper<Enterprise> {

		/**
		 * (non-Javadoc)
		 * @Title: mapRow
		 * @Description: 
		 * @see
		 * @author youjun @date 2017-08-03 11:01:33
		 */
		@Override
		public Enterprise mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
			final Enterprise enterprise = new Enterprise();
			enterprise.setId(resultSet.getInt("_id"));
			enterprise.setBasicModuleId(resultSet.getInt("_basic_module_id"));
			enterprise.setParentId(resultSet.getInt("_parent_id"));
			enterprise.setCode(resultSet.getString("_code"));
			enterprise.setName(resultSet.getString("_name"));
			enterprise.setMaxProductCnt(resultSet.getInt("_max_product_cnt"));
			enterprise.setMaxUserCnt(resultSet.getInt("_max_user_cnt"));
			enterprise.setMaxTerminalCnt(resultSet.getInt("_max_terminal_cnt"));
			//enterprise.setMaxTagCnt(resultSet.getLong("_max_tag_cnt"));
			enterprise.setStatus(resultSet.getShort("_status"));
			enterprise.setRemarks(resultSet.getString("_remarks"));
			enterprise.setBasicModuleParentId(resultSet.getInt("_basic_module_parent_id"));
			enterprise.setCreateTime(resultSet.getTimestamp("_create_time"));
			enterprise.setUpdateTime(resultSet.getTimestamp("_update_time"));
			return enterprise;
		}
	}


}
